package tt;

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import static java.util.Comparator.comparingInt;

public class Second {
    static class Interval {
        int start;
        int end;

        Interval() {
            start = 0;
            end = 0;
        }

        Interval(int s, int e) {
            start = s;
            end = e;
        }
    }
    public static void second() {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Interval> list = new LinkedList<Interval>();
        for (int i = 0; i < n; i++) {
            String str = sc.next();
            String[] strings = str.split(";");
            for (String string : strings) {
                String[] strings1 = string.split(",");
                int begin = Integer.parseInt(strings1[0]);
                int end = Integer.parseInt(strings1[1]);
                list.add(new Interval(begin, end));
            }
        }
        merge(list);
    }

    public static void merge(List<Interval> intervals) {

        intervals.sort(comparingInt(i -> i.start));
        StringBuilder sb = new StringBuilder();
        int start = intervals.get(0).start;
        int end = intervals.get(0).end;
        List<Interval> result = new LinkedList<Interval>();
        for (Interval interval : intervals) {
            if (interval.start <= end) // Overlapping intervals, move the end if needed
                end = Math.max(end, interval.end);
            else {                     // Disjoint intervals, add the previous one and reset bounds
                result.add(new Interval(start, end));
                start = interval.start;
                end = interval.end;
            }
        }

        // Add the last interval
        result.add(new Interval(start, end));
        for (Interval interval : result) {
            if (sb.length() != 0) sb.append(";");
            sb.append(interval.start).append(",");
            sb.append(interval.end);
        }
        System.out.println(sb.toString());
    }

}
